#include "timer.h"
#include "gpio.h"
#include "work.h"
#include "bt.h"
#include "peripheral.h"

uint8_t ms_cnt,Ems_Duty,en_cnt,ems_pulse=0,b_cnt,touch_delay;
uint16_t fan_cnt,fan_cnt1,ems_cnt;
boolean_t ms10_f,ms10_f1,ms10_f2,ms10_f3;
extern uint8_t Work_Mode,Beep_Time,Error_State,phase_cnt,Ems_Wave;
extern uint16_t sub_period;
extern boolean_t Trigger_f,beep_f,ems_f,touch_f;

void delay_us(uint16_t us)
{
		uint32_t i;
		while(us--)
		{
				__NOP();
		}
}

void delay_ms(uint16_t ms)
{
		while(ms--)
		{
				delay_us(800);
		}
}

void App_Timer0Cfg(uint16_t us)
{
		uint16_t                  u16ArrValue;
    uint16_t                  u16CntValue;
    stc_bt_mode23_cfg_t     stcBtBaseCfg;
    
    DDL_ZERO_STRUCT(stcBtBaseCfg);
    
    Sysctrl_SetPeripheralGate(SysctrlPeripheralBTim, TRUE); //Base Timer����ʱ��ʹ��
    
    stcBtBaseCfg.enWorkMode = BtWorkMode3;                  //��ʱ��ģʽ
    stcBtBaseCfg.enCT       = BtTimer;                      //��ʱ�����ܣ�����ʱ��Ϊ�ڲ�PCLK
    stcBtBaseCfg.enPRS      = BtPCLKDiv16;                 //PCLK/256
		stcBtBaseCfg.enPWMTypeSel  = BtIndependentPWM;         //�������PWM     //BtComplementaryPWM;
    stcBtBaseCfg.enPWM2sSel    = BtSinglePointCmp;         //����ȽϹ���
    stcBtBaseCfg.bOneShot      = FALSE;                    //ѭ������
    stcBtBaseCfg.bURSSel       = FALSE;                    //���������
	
		Bt_Mode23_Init(TIM0, &stcBtBaseCfg);                   //TIM0 ��ģʽ23���ܳ�ʼ��
	
    Bt_M23_ARRSet(TIM0, us*1.58, TRUE);                  //��������ֵ,��ʹ�ܻ���

		Bt_M23_SetValidPeriod(TIM0,1);
    Bt_M23_Cnt16Set(TIM0, 0); 
		
    Bt_ClearAllIntFlag(TIM0);                         //���жϱ�־   
    Bt_Mode23_EnableIrq(TIM0,BtUevIrq);                               //ʹ��TIM0�ж�(ģʽ0ʱֻ��һ���ж�)
    EnableNvic(TIM0_IRQn, IrqLevel0, TRUE);                 //TIM0�ж�ʹ��

		Bt_M23_Run(TIM0);        //TIM0 ���С�
}

void Tim0_IRQHandler(void)	//50us
{
    if(TRUE == Bt_GetIntFlag(TIM0, BtUevIrq))
    {
				ms_cnt++;
				if(ms_cnt>199)
				{
						ms_cnt=0;
						ms10_f=1;
						ms10_f1=1;
						ms10_f2=1;
						ms10_f3=1;
				}
				if(Work_Mode)
				{
						if(FAN_CHECK)
						{
								fan_cnt1=0;
								if(fan_cnt<20000)
								fan_cnt++;
								if(fan_cnt>15000)
								{
										Error_State|=FAN_ERROR;
										Work_Mode=0;
								}
						}
						else
						{
								fan_cnt=0;
								if(fan_cnt1<20000)
								fan_cnt1++;
								if(fan_cnt1>15000)
								{
										Error_State|=FAN_ERROR;
										Work_Mode=0;
								}
						}
				}
				else
				{
						fan_cnt=0;
						fan_cnt1=0;
				}
				if(beep_f)
				{
						b_cnt++;
						if(b_cnt>4)
						{
								b_cnt=0;
								BUZZER_OFF;
						}
						else if(b_cnt>1)
						{
								BUZZER_ON;
						}
				}	
				if(ems_pulse==1||ems_pulse==4||ems_pulse==7)
				{
						EMS_PWM_N_OFF;
						EMS_PWM_P_ON;
						ems_pulse++;
				}
				else if(ems_pulse==2||ems_pulse==5||ems_pulse==8)
				{
						EMS_PWM_P_OFF;
						EMS_PWM_N_ON;
						ems_pulse++;
						touch_f=0;
						touch_delay=0;
				}
				else 
				{
						EMS_PWM_N_OFF;
						EMS_PWM_P_OFF;
						if(ems_pulse==3||ems_pulse==6)
						{
								ems_pulse++;
						}
						else
						{
								ems_pulse=0;
						}
				}
				if(ems_f)
				{
						ems_cnt++;
						if(ems_cnt>sub_period)
						{
								ems_pulse=1;
								Touch1_ON;			//关闭触摸
								ems_cnt=0;
						}
				}		
				if(touch_f==0)
				{
						if(touch_delay<50)
						touch_delay++;
						else
						touch_f=1;
				}
				Bt_ClearIntFlag(TIM0,BtUevIrq); //清楚中断标记位
		}
}
//systick�жϺ���
void SysTick_IRQHandler(void)
{
   
}
